package main

//#include <stdlib.h>
//#include "printer.h"
import "C"
import (
	"encoding/json"
	"time"
)

// 注意：CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go run . (顺利运行)
// 注意：CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go run main.go (不能执行)

//export EncodeF64
func EncodeF64(key *C.char, value float64) *C.char {
	// defer C.free(unsafe.Pointer(cstr))

	m := map[string]float64{
		C.GoString(key): value,
	}

	b, _ := json.Marshal((&m))
	// warnning: this is a memory leak
	cstr := C.CString(string(b))

	return cstr
}

//export RunCallback
func RunCallback() {
	for i := range 100 {
		C.briderFn(C.int(i))
		time.Sleep(1 * time.Second)
	}
}

//export RecvMsgCallback
func RecvMsgCallback() {
	buf := []byte("hello")
	for i := range 100 {
		// bufC := C.CBytes(buf)
		// defer C.free(unsafe.Pointer(bufC))
		//C.RecvMsgBrider(C.int(i), unsafe.Pointer(bufC), C.int(len(buf)))
		C.RecvMsgBrider(C.int(i), (*C.uchar)(&buf[0]), C.int(len(buf)))
		time.Sleep(1 * time.Second)
	}
}

func main() {
	// fmt.Printf("{PI:%f}\n", C.M_PI)
	// C.printPiJSON()

	// RunCallback()
}
